﻿using System;
using System.Collections.Generic;
using System.Text;
using SMBL.Interface.LifeCycle;
using SMBL.Interface.Document;
using SMBL.Interface.Operation;


namespace SMBL.Operation
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class DocOperation : IOperation
    {
        ////////////////////////////////////////////////////////////////////////
        // Parameters
        ////////////////////////////////////////////////////////////////////////

        // Last Error ID
        // Just store the error occurred during the last processing
        // Modify only by SetErrorId
        private long myLastError;

        // Processor
        protected Interface.Document.IDocProcessor myProcessor;
        private Boolean hasProcessor = false;

        // Database Name
        protected String myBindFilePath;
        public String BindDBFilePath
        {
            set { myBindFilePath = value; }
        }

        ////////////////////////////////////////////////////////////////////////
        // Interface
        ////////////////////////////////////////////////////////////////////////

        #region IOperation Members

        /// <summary>
        /// This function is used to be invoked by Control Center
        /// @Author: Push
        /// </summary>
        public virtual void processDelegate()
        {
            throw new Exception("This is Root Operation Class, Can not be used to do anything.");
        }

        /// <summary>
        /// Get a processor from control center
        /// @Author: Push
        /// </summary>
        /// <param name="ObjProcessor"></param>
        public void SetProcessor(object ObjProcessor)
        {
            if (ObjProcessor != null)
            {
                this.myProcessor = (SMBL.Interface.Document.IDocProcessor)ObjProcessor;
                this.hasProcessor = true;
            }
        }

        /// <summary>
        /// Release the processor I held
        /// @Author: Push
        /// </summary>
        public void ReleaseProcessor()
        {
            if (this.hasProcessor)
            {
                // Mark the connection to disconnect;
                this.hasProcessor = false;
                this.myProcessor = null;
                //return this.myProvider;
            }
        }

        /// <summary>
        /// Return my binded file path
        /// @Author: Push
        /// </summary>
        /// <returns></returns>
        public string GetBindString()
        {
            return myBindFilePath;
        }

        /// <summary>
        /// Registe with control center and invoke the call back function
        /// @Author: Push
        /// </summary>
        /// <returns></returns>
        public bool Do()
        {
            // Registe the operation
            if (!Control.ControlCenter.DocRegister(this)) return false;
            Boolean tmpResult = Control.ControlCenter.DoUnderControl(this);
            // Unregiste
            Control.ControlCenter.DocUnRegister(this);
            return tmpResult;
        }

        #endregion

        #region IOpError Members

        /// <summary>
        /// Set my last error's Id
        /// @Author: Push
        /// </summary>
        /// <param name="ErrorId"></param>
        public void SetLastErrorId(long ErrorId)
        {
            myLastError = ErrorId;
        }

        /// <summary>
        /// Return my last error's message
        /// @Author: Push
        /// </summary>
        /// <returns></returns>
        public string GetLastError()
        {
            return Core.ErrorSystem.GetError(myLastError);
        }

        #endregion
    }
}
